﻿#region Copyright (c) 2013-08, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;


namespace Amarok.Agents
{
	/// <summary>
	/// This type provides hooks (events) for diagnosis purposes.
	/// </summary>
	public static class AgentHooks
	{
		#region ++ Public Static Interface (Events) ++

		/// <summary>
		/// Occurs when a message was published by an agent.
		/// </summary>
		public static event EventHandler<MessagePublishedEventArgs> MessagePublished;

		/// <summary>
		/// Occurs when a message was posted to an agent, but only if the message is scheduled for further 
		/// processing, meaning a matching message handler exists.
		/// </summary>
		public static event EventHandler<MessagePostedEventArgs> MessagePosted;


		/// <summary>
		/// Occurs when execution of a message handler started.
		/// </summary>
		public static event EventHandler<MessageHandlerStartedEventArgs> MessageHandlerStarted;

		/// <summary>
		/// Occurs when execution of a message handler finished.
		/// </summary>
		public static event EventHandler<MessageHandlerFinishedEventArgs> MessageHandlerFinished;

		/// <summary>
		/// Occurs when execution of a message handler finished with an exception.
		/// </summary>
		public static event EventHandler<MessageHandlerFailedEventArgs> MessageHandlerFailed;


		/// <summary>
		/// Occurs when execution of a information handler started.
		/// </summary>
		public static event EventHandler<MessageHandlerStartedEventArgs> InformationHandlerStarted;

		/// <summary>
		/// Occurs when execution of a information handler finished.
		/// </summary>
		public static event EventHandler<MessageHandlerFinishedEventArgs> InformationHandlerFinished;

		/// <summary>
		/// Occurs when execution of a information handler finished with an exception.
		/// </summary>
		public static event EventHandler<MessageHandlerFailedEventArgs> InformationHandlerFailed;


		/// <summary>
		/// Occurs when execution of an operation handler started.
		/// </summary>
		public static event EventHandler<MessageHandlerStartedEventArgs> OperationHandlerStarted;

		/// <summary>
		/// Occurs when execution of an operation handler finished.
		/// </summary>
		public static event EventHandler<MessageHandlerFinishedEventArgs> OperationHandlerFinished;

		/// <summary>
		/// Occurs when execution of an operation handler finished with an exception.
		/// </summary>
		public static event EventHandler<MessageHandlerFailedEventArgs> OperationHandlerFailed;


		/// <summary>
		/// Removes all event handler
		/// </summary>
		public static void Reset()
		{
			MessagePublished = null;
			MessagePosted = null;

			MessageHandlerStarted = null;
			MessageHandlerFinished = null;
			MessageHandlerFailed = null;

			InformationHandlerStarted = null;
			InformationHandlerFinished = null;
			InformationHandlerFailed = null;

			OperationHandlerStarted = null;
			OperationHandlerFinished = null;
			OperationHandlerFailed = null;
		}

		#endregion

		#region ~~ Internal Static Interface ~~

		/// <summary>
		/// </summary>
		internal static void RaiseMessagePublished(IAgent agent, Message message)
		{
			try
			{
				var handler = MessagePublished;

				if (handler != null)
				{
					handler(null, new MessagePublishedEventArgs(agent, message));
				}
			}
			catch (Exception)
			{
				// ignore
			}
		}

		/// <summary>
		/// </summary>
		internal static void RaiseMessagePosted(IAgent agent, Message message)
		{
			try
			{
				var handler = MessagePosted;

				if (handler != null)
				{
					handler(null, new MessagePostedEventArgs(agent, message));
				}
			}
			catch (Exception)
			{
				// ignore
			}
		}


		/// <summary>
		/// </summary>
		internal static void RaiseMessageHandlerStarted(IAgent agent, Message message, Delegate handler)
		{
			try
			{
				var callback = MessageHandlerStarted;

				if (callback != null)
				{
					callback(null, new MessageHandlerStartedEventArgs(agent, message, handler));
				}
			}
			catch (Exception)
			{
				// ignore
			}
		}

		/// <summary>
		/// </summary>
		internal static void RaiseMessageHandlerFinished(IAgent agent, Message message, Delegate handler)
		{
			try
			{
				var callback = MessageHandlerFinished;

				if (callback != null)
				{
					callback(null, new MessageHandlerFinishedEventArgs(agent, message, handler));
				}
			}
			catch (Exception)
			{
				// ignore
			}
		}

		/// <summary>
		/// </summary>
		internal static void RaiseMessageHandlerFailed(IAgent agent, Message message, Delegate handler, Exception exception)
		{
			try
			{
				var callback = MessageHandlerFailed;

				if (callback != null)
				{
					callback(null, new MessageHandlerFailedEventArgs(agent, message, handler, exception));
				}
			}
			catch (Exception)
			{
				// ignore
			}
		}


		/// <summary>
		/// </summary>
		internal static void RaiseInformationHandlerStarted(IAgent agent, Message message, Delegate handler)
		{
			try
			{
				var callback = InformationHandlerStarted;

				if (callback != null)
				{
					callback(null, new MessageHandlerStartedEventArgs(agent, message, handler));
				}
			}
			catch (Exception)
			{
				// ignore
			}
		}

		/// <summary>
		/// </summary>
		internal static void RaiseInformationHandlerFinished(IAgent agent, Message message, Delegate handler)
		{
			try
			{
				var callback = InformationHandlerFinished;

				if (callback != null)
				{
					callback(null, new MessageHandlerFinishedEventArgs(agent, message, handler));
				}
			}
			catch (Exception)
			{
				// ignore
			}
		}

		/// <summary>
		/// </summary>
		internal static void RaiseInformationHandlerFailed(IAgent agent, Message message, Delegate handler, Exception exception)
		{
			try
			{
				var callback = InformationHandlerFailed;

				if (callback != null)
				{
					callback(null, new MessageHandlerFailedEventArgs(agent, message, handler, exception));
				}
			}
			catch (Exception)
			{
				// ignore
			}
		}


		/// <summary>
		/// </summary>
		internal static void RaiseOperationHandlerStarted(IAgent agent, Message message, Delegate handler)
		{
			try
			{
				var callback = OperationHandlerStarted;

				if (callback != null)
				{
					callback(null, new MessageHandlerStartedEventArgs(agent, message, handler));
				}
			}
			catch (Exception)
			{
				// ignore
			}
		}

		/// <summary>
		/// </summary>
		internal static void RaiseOperationHandlerFinished(IAgent agent, Message message, Delegate handler)
		{
			try
			{
				var callback = OperationHandlerFinished;

				if (callback != null)
				{
					callback(null, new MessageHandlerFinishedEventArgs(agent, message, handler));
				}
			}
			catch (Exception)
			{
				// ignore
			}
		}

		/// <summary>
		/// </summary>
		internal static void RaiseOperationHandlerFailed(IAgent agent, Message message, Delegate handler, Exception exception)
		{
			try
			{
				var callback = OperationHandlerFailed;

				if (callback != null)
				{
					callback(null, new MessageHandlerFailedEventArgs(agent, message, handler, exception));
				}
			}
			catch (Exception)
			{
				// ignore
			}
		}

		#endregion

		#region ++ Public Static Interface (Event Args) ++

		/// <summary>
		/// </summary>
		public abstract class AgentHooksEventArgs : EventArgs
		{
			/// <summary>
			/// </summary>
			public DateTime TimestampUtc
			{
				get;
				private set;
			}

			/// <summary>
			/// </summary>
			public IAgent Agent
			{
				get;
				private set;
			}

			/// <summary>
			/// </summary>
			public Message Message
			{
				get;
				private set;
			}


			/// <summary>
			/// </summary>
			protected AgentHooksEventArgs(IAgent agent, Message message)
			{
				this.TimestampUtc = DateTime.UtcNow;
				this.Agent = agent;
				this.Message = message;
			}
		}


		/// <summary>
		/// </summary>
		public sealed class MessagePublishedEventArgs : AgentHooksEventArgs
		{
			/// <summary>
			/// </summary>
			public MessagePublishedEventArgs(IAgent agent, Message message)
				: base(agent, message)
			{
			}
		}

		/// <summary>
		/// </summary>
		public sealed class MessagePostedEventArgs : AgentHooksEventArgs
		{
			/// <summary>
			/// </summary>
			public MessagePostedEventArgs(IAgent agent, Message message)
				: base(agent, message)
			{
			}
		}


		/// <summary>
		/// </summary>
		public sealed class MessageHandlerStartedEventArgs : AgentHooksEventArgs
		{
			/// <summary>
			/// </summary>
			public Delegate Handler
			{
				get;
				private set;
			}


			/// <summary>
			/// </summary>
			public MessageHandlerStartedEventArgs(IAgent agent, Message message, Delegate handler)
				: base(agent, message)
			{
				this.Handler = handler;
			}
		}

		/// <summary>
		/// </summary>
		public sealed class MessageHandlerFinishedEventArgs : AgentHooksEventArgs
		{
			/// <summary>
			/// </summary>
			public Delegate Handler
			{
				get;
				private set;
			}


			/// <summary>
			/// </summary>
			public MessageHandlerFinishedEventArgs(IAgent agent, Message message, Delegate handler)
				: base(agent, message)
			{
				this.Handler = handler;
			}
		}

		/// <summary>
		/// </summary>
		public sealed class MessageHandlerFailedEventArgs : AgentHooksEventArgs
		{
			/// <summary>
			/// </summary>
			public Delegate Handler
			{
				get;
				private set;
			}

			/// <summary>
			/// </summary>
			public Exception Exception
			{
				get;
				private set;
			}


			/// <summary>
			/// </summary>
			public MessageHandlerFailedEventArgs(IAgent agent, Message message, Delegate handler, Exception exception)
				: base(agent, message)
			{
				this.Handler = handler;
				this.Exception = exception;
			}
		}

		#endregion

	}
}
